
// @ts-nocheck
import {fileUploadOssPolicy} from "../api/common/img";
import {v4} from "uuid";

export const UploadImg = async (callback,type) => {
    console.log('我写的回调',callback)
    const {code, data} = await fileUploadOssPolicy()
    if (code != 'A000') {
        uni.showToast({
            title: '上传失败',
            icon: 'loading',
            duration: 2000
        })
        return code
    }
    // console.log('我写的data',data);
    // return;
    let ossData = data;
    uni.chooseImage({
        sourceType: ['album', 'camera'],
        count: type ? 1 : '',
        success: function (res) {
            // console.log(res, '上传');
            const tempFilePath = res.tempFilePaths;
            // 上传图片
            for (let n = 0; n < tempFilePath.length; n++) {
                let ossUrl = ossData.host;
                let str = tempFilePath[n].substring(tempFilePath[n].lastIndexOf('.'));
                const filePath = tempFilePath[n];

                let nameStr = ossData.dir + v4() + str;

                console.log('tempFilePath[n]::', filePath)
                uni.uploadFile({
                    url: ossUrl, //这个是你服务上的php用来处理图片上传的路径（可var_dump($_FILE)出来看看）
                    filePath: filePath,
                    name: 'file',
                    formData: {
                        name: nameStr,
                        key: nameStr,
                        policy: ossData.policy, // 输入你获取的的policy
                        OSSAccessKeyId: ossData.accessId, // 输入你的AccessKeyId
                        success_action_status: '200', // 让服务端返回200,不然，默认会返回204
                        signature: ossData.signature // 输入你获取的的signature
                    },
                    header: {
                        'content-type': 'multipart/form-data'
                    }, //post方法必须写
                    success: function (res) { //成功回调函数
                        console.log(ossUrl + "/" + nameStr, '?????');
                        callback(ossUrl + "/" + nameStr)

                    },
                    fail: function (e) {
                        uni.showToast({
                            title: '上传失败',
                            icon: 'loading',
                            duration: 2000
                        })
                    },
                    complete: function () {
                        uni.hideToast(); //隐藏Toast
                        if (n == tempFilePath.length) {
                            uni.showToast({
                                title: '上传成功',
                                icon: 'success',
                                duration: 2000
                            })
                        }
                    }
                })
            }
        }
    });
}

export const UploadImgLocal = async (callback, filePathLocal: string) => {
    console.log(callback,'11111');
    console.log(filePathLocal,'2222222');
    const {code, data} = await fileUploadOssPolicy()
    if (code != 'A000') {
        uni.showToast({
            title: '上传失败',
            icon: 'loading',
            duration: 2000
        })
        return code
    }

    let ossUrl = data.host;

    let str = filePathLocal.substring(filePathLocal.lastIndexOf('.'));
    let nameStr = data.dir + v4() + str;

    uni.uploadFile({
        url: ossUrl, //这个是你服务上的php用来处理图片上传的路径（可var_dump($_FILE)出来看看）
        filePath: filePathLocal,
        name: 'file',
        formData: {
            name: nameStr,
            key: nameStr,
            policy: data.policy, // 输入你获取的的policy
            OSSAccessKeyId: data.accessId, // 输入你的AccessKeyId
            success_action_status: '200', // 让服务端返回200,不然，默认会返回204
            signature: data.signature // 输入你获取的的signature
        },
        header: {
            'content-type': 'multipart/form-data'
        }, //post方法必须写
        success: function (res) { //成功回调函数
            // console.log(ossUrl + "/" + nameStr,'?????');
            callback(ossUrl + "/" + nameStr)
        },
        fail: function (e) {
            uni.showToast({
                title: '上传失败',
                icon: 'loading',
                duration: 2000
            })
        },
        complete: function (result) {
            console.log('res:', result)
            uni.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 2000
            })
        }
    })
}


// 删除图片
// const removeImage= (index,filename,callback) =>{
//   // var that = this;
//   // var index = e.currentTarget.dataset.index;
//   wx.showModal({
//     title: '提示',
//     content: '确定删除图片',
//     success(res) {
//       filename.splice(index, 1);
//       callback(res)
//     }
//   })
// }

// 上传视频
export const UploadVideo = (ossData, callback,) => {
    uni.chooseVideo({
        maxDuration: 60,
        // count: 1,
        success: (responent) => {
            var tempFilePath = [responent.tempFilePath];
            for (var n = 0; n < tempFilePath.length; n++) {
                let tiemr = new Date();
                let address = tiemr.getFullYear() + '' + (tiemr.getMonth() + 1) + '' + tiemr.getDate();
                address = ossData.dir + address + '/';
                let ossUrl = ossData.host;
                let str = tempFilePath[n].substr(tempFilePath[n].lastIndexOf('.'));
                let nameStr = address + tiemr.getTime() + str;
                var filePath = tempFilePath[n];
                uni.uploadFile({
                    url: ossUrl, //这个是你服务上的php用来处理图片上传的路径（可var_dump($_FILE)出来看看）
                    filePath: filePath,
                    name: 'file',
                    formData: {
                        name: nameStr,
                        key: nameStr,
                        policy: ossData.policy, // 输入你获取的的policy
                        OSSAccessKeyId: ossData.accessId, // 输入你的AccessKeyId
                        success_action_status: '200', // 让服务端返回200,不然，默认会返回204
                        signature: ossData.signature // 输入你获取的的signature
                    },
                    // method: 'POST', //post必须大写
                    header: {
                        'content-type': 'multipart/form-data'
                    }, //post方法必须写
                    success: function (res) { //成功回调函数
                        callback(ossUrl + "/" + nameStr)

                    },
                    fail: function (e) {
                        uni.showToast({
                            title: '上传失败',
                            icon: 'loading',
                            duration: 2000
                        })
                    },
                    complete: function () {
                        uni.hideToast(); //隐藏Toast
                        if (n == tempFilePath.length) {
                            uni.showToast({
                                title: '上传成功',
                                icon: 'success',
                                duration: 2000
                            })
                        }
                    }
                })
            }
        }
    })
}
